package com.xbongbong.paas.help.menu;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.admin.pojo.dto.MirrorRollBackDTO;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.constant.MessageConstant;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.PaasAppEntity;
import com.xbongbong.paas.domain.entity.PaasFormEntity;
import com.xbongbong.paas.domain.entity.PaasMenuEntity;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.enums.MenuTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.help.CommonHelp;
import com.xbongbong.paas.model.PaasFormModel;
import com.xbongbong.paas.pojo.SummaryDataPoJo;
import com.xbongbong.paas.pojo.dto.FormEntityAddDTO;
import com.xbongbong.paas.pojo.dto.FormExplainAddDTO;
import com.xbongbong.paas.pojo.dto.MenuFormAddDTO;
import com.xbongbong.paas.pojo.dto.MenuGroupAddDTO;
import com.xbongbong.paas.service.PaasFormExplainService;
import com.xbongbong.paas.service.PaasFormService;
import com.xbongbong.paas.service.PaasMenuService;
import com.xbongbong.paas.service.init.AbstractInitMenuHelp;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.pro.constant.I18nStringConstant;
import com.xbongbong.pro.enums.AccessLinkEnum;
import com.xbongbong.pro.enums.DistributorMarkEnum;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.enums.fund.enums.ModelTypeEnum;
import com.xbongbong.pro.enums.menu.enums.FundManagementMenuEnum;
import com.xbongbong.pro.enums.menu.enums.SystemAppMenuEnum;
import com.xbongbong.pro.form.pojo.dto.FormAddBatchDTO;
import com.xbongbong.pro.form.pojo.vo.FormAddBatchVO;
import com.xbongbong.pro.formexplain.pojo.dto.FormExplainAddBatchDTO;
import com.xbongbong.pro.formexplain.pojo.vo.FormExplainAddBatchVO;
import com.xbongbong.pro.fundset.pojo.FundSetTransitionPojo;
import com.xbongbong.pro.menu.pojo.dto.MenuFormAddBatchDTO;
import com.xbongbong.pro.menu.pojo.dto.MenuGroupAddBatchDTO;
import com.xbongbong.pro.menu.pojo.dto.MenuInitDTO;
import com.xbongbong.pro.menu.pojo.vo.MenuFormAddBatchVO;
import com.xbongbong.pro.menu.pojo.vo.MenuGroupAddBatchVO;
import com.xbongbong.pro.menu.pojo.vo.MenuInitVO;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.enums.CustomerStatementStatisticDistributorEnum;
import com.xbongbong.saas.enums.CustomerStatementStatisticEnum;
import com.xbongbong.saas.enums.MirrorModuleEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.FundTransferEnum;
import com.xbongbong.saas.enums.business.OtherExpenseEnum;
import com.xbongbong.saas.enums.business.OtherIncomeEnum;
import com.xbongbong.saas.script.help.InitExplainsHelper;
import com.xbongbong.saas.service.toolbox.help.FundSetHelp;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 资金管理应用生成和菜单初始化
 * @author 吴峰
 * @version v1.0
 * @date 2019/1/17 19:46
 * @since v1.0
 */
@Component
public class InitFundManagementMenuHelp extends AbstractInitMenuHelp {

    private static final Logger LOG = LoggerFactory.getLogger(InitFundManagementMenuHelp.class);

    @Resource
    private PaasMenuService paasMenuService;
    @Resource
    private PaasFormService paasFormService;
    @Resource
    private PaasFormExplainService paasFormExplainService;
    @Resource
    private InitMenuHelp initMenuHelp;
    @Resource
    private FundSetHelp fundSetHelp;
    @Resource
    private PaasFormModel paasFormModel;
    @Resource
    private CommonHelp commonHelp;

    @Override
    public List<String> alias() {
        return Arrays.asList(SystemAppMenuEnum.FUND_MANAGEMENT.getAlias());
    }
    @Override
    public MenuInitVO initApp(MenuInitDTO menuInitDTO, Map<String, PaasAppEntity> appMap) throws XbbException {
        return new MenuInitVO();
    }

    /**
     * 资金管理菜单初始化
     * @author 吴峰
     * @date 2019/1/24 18:44
     * @param menuInitDTO
     * @return void
     * @throws XbbException
     */
    @Override
    public MenuInitVO initMenu(MenuInitDTO menuInitDTO, Map<String, PaasAppEntity> appMap) throws XbbException {
        MenuInitVO menuInitVO = new MenuInitVO();
        try {
            // 获取下应用id
            menuInitDTO.setAppId(appMap.get(SystemAppMenuEnum.FUND_MANAGEMENT.getAlias()).getId());

            // 插入菜单分组
            MenuGroupAddBatchVO crmGroupAddBatchVO = groupAddBatch(menuInitDTO);
            if(Objects.isNull(crmGroupAddBatchVO)){
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
            }
            // 插入除分组外对应的菜单
            MenuFormAddBatchVO menuFormAddBatchVO = formAddBatch(menuInitDTO, crmGroupAddBatchVO);
            if (Objects.isNull(menuFormAddBatchVO)) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
            }
            List<PaasMenuEntity> menuGroupList = crmGroupAddBatchVO.getMenuList();
            List<PaasMenuEntity> menuList = menuFormAddBatchVO.getMenuList();
            menuList.addAll(menuGroupList);
            // 插入菜单对应的表单
            FormAddBatchDTO formAddBatchDTO = new FormAddBatchDTO();
            BeanUtil.copyProperties(menuInitDTO, formAddBatchDTO);
            List<FormEntityAddDTO> formList = new ArrayList<>();
            // 通过分组菜单，得到需要插入的表单列表
            getFormList(formList, menuList);
            formAddBatchDTO.setFormList(formList);
            FormAddBatchVO formAddBatchVO = paasFormService.addBatch(formAddBatchDTO);
            if (Objects.isNull(formAddBatchVO)) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
            }
            // 插入表单对应的字段解释
             formExplainAddBatch(menuInitDTO, formAddBatchVO);
            menuInitVO.setFormList(formAddBatchVO.getFormList());
        } catch (XbbException e){
            throw e;
        } catch (Exception e){
            LOG.error("InitCrmMenuHelp.menuInit初始化菜单出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return menuInitVO;
    }

    @Override
    public void rollBackInit(MirrorRollBackDTO mirrorRollBackDTO, List<Long> installFormList, PaasFormEntity paasFormEntity) throws XbbException {
        return;
    }


    /**
     * 添加表单字段解释
     * @author 吴峰
     * @date 2019/1/24 18:44
     * @param menuInitDTO
     * @param formAddBatchVO 表单数据
     * @return 返回表单字段对象
     * @throws XbbException
     */
    public FormExplainAddBatchVO formExplainAddBatch(MenuInitDTO menuInitDTO, FormAddBatchVO formAddBatchVO) throws XbbException {
        //原来的方法默认生成非经销商的解释，且默认为“手动创建应收”模式
        return formExplainAddBatch(menuInitDTO, formAddBatchVO, DistributorMarkEnum.OTHER, ModelTypeEnum.PLAN);
    }
    /**
     * 添加表单字段解释
     * @param menuInitDTO
     * @param formAddBatchVO 表单数据
     * @param distributorMarkEnum 经销商标识
     * @param modelTypeEnum 生成规则
     * @return com.xbongbong.pro.formexplain.pojo.vo.FormExplainAddBatchVO
     * @throws XbbException
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    public FormExplainAddBatchVO formExplainAddBatch(MenuInitDTO menuInitDTO, FormAddBatchVO formAddBatchVO, DistributorMarkEnum distributorMarkEnum, ModelTypeEnum modelTypeEnum) throws XbbException {
        FormExplainAddBatchVO formExplainAddBatchVO;
        try {
            Integer distributorMark = distributorMarkEnum.getCode();
            List<PaasFormEntity> formResultList = formAddBatchVO.getFormList();
            FormExplainAddBatchDTO formExplainAddBatchDTO = new FormExplainAddBatchDTO();
            BeanUtil.copyProperties(menuInitDTO, formExplainAddBatchDTO);
            List<FormExplainAddDTO> formExplainList = new ArrayList<>();
            FundSetTransitionPojo fundSetTransitionPojo = new FundSetTransitionPojo(menuInitDTO.getCorpid());
            //非“手动创建应收”模式标识，explain可能初始化为[]，但是非“手动创建应收”模式需要处理特定表单的字段
            boolean noPlanFlag = Objects.nonNull(modelTypeEnum) && !Objects.equals(modelTypeEnum, ModelTypeEnum.PLAN);
            //切换应收规则（非“手动创建应收”模式）时要处理的业务list
            List<Integer> specialBusinessList = fundSetHelp.getSpecialBusinessList();
            for (PaasFormEntity item : formResultList) {
                FormExplainAddDTO formExplainAddDTO = new FormExplainAddDTO();
                BeanUtil.copyProperties(item, formExplainAddDTO);
                formExplainAddDTO.setFormId(item.getId());
                String alias = XbbRefTypeEnum.getByCode(item.getBusinessType()).getAlias();
                // 初始化方法获取字段列表
                List<FieldAttrEntity> explainList = new ArrayList<>();
                //非“手动创建应收”模式并且需要处理表单解释
                boolean flag = noPlanFlag && specialBusinessList.contains(item.getBusinessType());
                if (flag || !InitExplainsHelper.EMPTY_EXPLAIN.contains(item.getBusinessType())) {
                    explainList = InitExplainsHelper.initFieldAttrEntity(alias, menuInitDTO.getProductForm(),fundSetTransitionPojo, distributorMark);
                }
                formExplainAddDTO.setFormList(explainList);
                //默认启用，防止不启用的菜单覆盖
                formExplainAddDTO.setEnable(1);
                formExplainAddDTO.setDistributorMark(distributorMark);
                formExplainList.add(formExplainAddDTO);
            }
            formExplainAddBatchDTO.setFormExplainList(formExplainList);
            formExplainAddBatchVO = paasFormExplainService.addBatch(formExplainAddBatchDTO);
        } catch (XbbException e) {
            throw e;
        } catch (Exception e) {
            LOG.error("InitCrmMenuHelp.formExplainAddBatch批量添加表单解释信息出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return formExplainAddBatchVO;
    }

    /**
     * 批量添加分组
     * @author 吴峰
     * @date 2019/1/24 18:43
     * @param menuInitDTO
     * @return com.xbongbong.pro.menu.pojo.vo.MenuGroupAddBatchVO
     * @throws XbbException
     */
    private MenuGroupAddBatchVO groupAddBatch(MenuInitDTO menuInitDTO) throws XbbException {
        MenuGroupAddBatchVO menuGroupAddBatchVO;
        try {
            Long appId = menuInitDTO.getAppId();
            MenuGroupAddBatchDTO menuGroupAddBatchDTO = new MenuGroupAddBatchDTO();
            BeanUtil.copyProperties(menuInitDTO, menuGroupAddBatchDTO);
            menuGroupAddBatchDTO.setAppId(appId);
            List<MenuGroupAddDTO> groupMenuList = new ArrayList<>();
            //信用管理，在线支付流水不初始化（只在经销商开启的时候初始化）
            List<Integer> unInitBusinessList = Arrays.asList(XbbRefTypeEnum.CREDIT_MANAGEMENT.getCode(), XbbRefTypeEnum.ONLINE_PAYMENT_FLOW.getCode());
            for(FundManagementMenuEnum fundManagementMenuEnum: FundManagementMenuEnum.values()){
                // 信用管理不初始化（在开启经销商时做初始化）
                if(fundManagementMenuEnum.getBelongAlias().equals(menuInitDTO.getAlias())
                        && StringUtil.isEmpty(fundManagementMenuEnum.getParent()) && !unInitBusinessList.contains(fundManagementMenuEnum.getCode())) {
                    MenuGroupAddDTO menuGroupAddDTO = new MenuGroupAddDTO();
                    BeanUtil.copyProperties(menuInitDTO, menuGroupAddDTO);
                    menuGroupAddDTO.setParentId(0L);
                    menuGroupAddDTO.setAppId(appId);
                    menuGroupAddDTO.setSaasMark(SaasMarkEnum.SAAS.getCode());
                    menuGroupAddDTO.setAlias(fundManagementMenuEnum.getAlias());
                    menuGroupAddDTO.setName(fundManagementMenuEnum.getName());
                    AccessLinkEnum accessLinkEnum = fundManagementMenuEnum.getAccessLinkEnum();
                    menuGroupAddDTO.setIcon(accessLinkEnum.getIcon());
                    menuGroupAddDTO.setColor(accessLinkEnum.getColor());
                    menuGroupAddDTO.setUrl(accessLinkEnum.getUrl());
                    menuGroupAddDTO.setMobileUrl(accessLinkEnum.getMobileUrl());
                    menuGroupAddDTO.setType(fundManagementMenuEnum.getType());
                    groupMenuList.add(menuGroupAddDTO);
                }
            }
            menuGroupAddBatchDTO.setMenuList(groupMenuList);
            menuGroupAddBatchVO = paasMenuService.addBatchGroupMenu(menuGroupAddBatchDTO);
        } catch (XbbException e) {
            throw e;
        } catch (Exception e) {
            LOG.error("InitCrmMenuHelp.groupAddBatch批量添加分组出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return menuGroupAddBatchVO;
    }

    /**
     * 批量添加子级菜单
     * @author 吴峰
     * @date 2019/1/24 18:43
     * @param menuInitDTO
     * @param menuGroupAddBatchVO
     * @return com.xbongbong.pro.menu.pojo.vo.MenuFormAddBatchVO
     * @throws XbbException
     */
    private MenuFormAddBatchVO formAddBatch(MenuInitDTO menuInitDTO, MenuGroupAddBatchVO menuGroupAddBatchVO) throws XbbException {
        MenuFormAddBatchVO menuFormAddBatchVO = new MenuFormAddBatchVO();
        try {
            Long appId = menuInitDTO.getAppId();
            Map<String, PaasMenuEntity> menuMap = new HashMap<>(menuGroupAddBatchVO.getMenuList().size());
            menuGroupAddBatchVO.getMenuList().forEach((item)->{
                menuMap.put(item.getAlias(), item);
            });
            MenuFormAddBatchDTO crmFormAddBatchDTO = new MenuFormAddBatchDTO();
            BeanUtil.copyProperties(menuInitDTO, crmFormAddBatchDTO);
            crmFormAddBatchDTO.setAppId(appId);
            List<MenuFormAddDTO> crmFormMenuList = new ArrayList<>();
            // 信用管理的不初始化（在开启经销商时做初始化）
            List<FundManagementMenuEnum> creditMenus = Arrays.asList(FundManagementMenuEnum.CREDIT_LIMIT, FundManagementMenuEnum.CREDIT_TEMPORARY_LIMIT, FundManagementMenuEnum.CREDIT_FIXED_LIMIT, FundManagementMenuEnum.CREDIT_CARD_BILL);
            for(FundManagementMenuEnum menuEnum: FundManagementMenuEnum.values()){
                if(menuEnum.getBelongAlias().equals(menuInitDTO.getAlias())
                        &&StringUtil.isNotEmpty(menuEnum.getParent()) && !creditMenus.contains(menuEnum)){
                    MenuFormAddDTO menuFormAddDTO = new MenuFormAddDTO();
                    BeanUtil.copyProperties(menuInitDTO, menuFormAddDTO);
                    menuFormAddDTO.setAppId(appId);
                    if(StringUtil.isEmpty(menuEnum.getParent())){
                        menuFormAddDTO.setParentId(0L);
                    }else{
                        menuFormAddDTO.setParentId(menuMap.get(menuEnum.getParent()).getId());
                    }
                    menuFormAddDTO.setSaasMark(SaasMarkEnum.SAAS.getCode());
                    menuFormAddDTO.setAlias(menuEnum.getAlias());
                    menuFormAddDTO.setName(menuEnum.getName());
                    AccessLinkEnum accessLinkEnum = menuEnum.getAccessLinkEnum();
                    menuFormAddDTO.setIcon(accessLinkEnum.getIcon());
                    menuFormAddDTO.setColor(accessLinkEnum.getColor());
                    menuFormAddDTO.setUrl(accessLinkEnum.getUrl());
                    menuFormAddDTO.setMobileUrl(accessLinkEnum.getMobileUrl());
                    menuFormAddDTO.setType(menuEnum.getType());
                    boolean preFlag = Objects.equals(menuEnum.getCode(),XbbRefTypeEnum.PREPAYMENT_BALANCE.getCode()) || Objects.equals(menuEnum.getCode(),XbbRefTypeEnum.PREPAY_BALANCE.getCode());
                    if(preFlag){
                        menuFormAddDTO.setEnable(0);
                    }
                    crmFormMenuList.add(menuFormAddDTO);
                }
            }
            if (CollectionsUtil.isNotEmpty(crmFormMenuList)) {
                crmFormAddBatchDTO.setMenuList(crmFormMenuList);
                menuFormAddBatchVO = paasMenuService.addBatchFormMenu(crmFormAddBatchDTO);
            }
        } catch (XbbException e) {
            throw e;
        } catch (Exception e) {
            LOG.error("InitCrmMenuHelp.formAddBatch批量添加表单出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return menuFormAddBatchVO;
    }

    /**
     * 根据菜单获取表单列表
     * @author 吴峰
     * @date 2019/1/24 18:43
     * @param formList 表单列表
     * @param menuList 菜单列表
     * @return 返回需要插入的表单列表
     * @throws XbbException 业务异常
     */
    public List<FormEntityAddDTO> getFormList(List<FormEntityAddDTO> formList, List<PaasMenuEntity> menuList) throws XbbException{
        //原来的方法默认生成非经销商的form
        return getFormList(formList, menuList, DistributorMarkEnum.OTHER);
    }
    /**
     * 根据菜单获取表单列表
     * @param formList 表单列表
     * @param menuList 菜单列表
     * @param distributorMarkEnum 经销商标识
     * @return java.util.List<com.xbongbong.paas.pojo.dto.FormEntityAddDTO>
     * @throws XbbException
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    public List<FormEntityAddDTO> getFormList(List<FormEntityAddDTO> formList, List<PaasMenuEntity> menuList, DistributorMarkEnum distributorMarkEnum) throws XbbException{
        try {
            //是否为经销商初始化
            boolean openWorkflow = false;
            if (CollectionUtils.isNotEmpty(menuList)) {
               openWorkflow = commonHelp.isOpenWorkFlow(menuList.get(0).getCorpid());
            }
            boolean distributorMarkFlag = Objects.equals(distributorMarkEnum, DistributorMarkEnum.DISTRIBUTOR);
            for (PaasMenuEntity item : menuList) {
                FormEntityAddDTO formEntityAddDTO = new FormEntityAddDTO();
                BeanUtil.copyProperties(item, formEntityAddDTO);
                FundManagementMenuEnum menuEnum = FundManagementMenuEnum.getByAlias(item.getAlias());
                if (Objects.isNull(menuEnum)) {
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.BUSINESS_TYPE_NOT_EXIST);
                }
                if(MenuTypeEnum.GROUP.getType().equals(menuEnum.getType()) || MenuTypeEnum.MENU.getType().equals(menuEnum.getType())) {
                    continue;
                }
                Integer businessType = menuEnum.getCode();
                formEntityAddDTO.setBusinessType(businessType);
                formEntityAddDTO.setSaasMark(SaasMarkEnum.SAAS.getCode());
                formEntityAddDTO.setCustomTitle(getAppDefaultTitle(businessType));
                formEntityAddDTO.setSummaryEntity(getAppDefaultSummary(businessType));
                formEntityAddDTO.setLabels(getAppDefaultLabel(businessType));
                formEntityAddDTO.setBusinessRules("0");
                formEntityAddDTO.setMenuId(item.getId());
                boolean flag = distributorMarkFlag && (Objects.equals(businessType, XbbRefTypeEnum.PAYMENT_SHEET.getCode()) || Objects.equals(businessType, XbbRefTypeEnum.INVOICE.getCode()) || Objects.equals(businessType, XbbRefTypeEnum.CREDIT_TEMPORARY_LIMIT.getCode()) || Objects.equals(businessType, XbbRefTypeEnum.CREDIT_FIXED_LIMIT.getCode()));
                if (flag && !openWorkflow) {
                    //经销商的回款单、销项发票默认开启审批流程
                    formEntityAddDTO.setIsProcessForm(BasicConstant.ONE);
                }
                if (distributorMarkFlag) {
                    //初始化时，名称特殊标识（非初始化则不能这样处理）
                    XbbRefTypeEnum xbbRefTypeEnum = XbbRefTypeEnum.getByCode(businessType);
                    String name = I18nMessageUtil.getMessage(I18nStringConstant.DISTRIBUTOR) + xbbRefTypeEnum.getName();
                    formEntityAddDTO.setName(name);
                    formEntityAddDTO.setSort(-1);
                    if (Objects.equals(menuEnum, FundManagementMenuEnum.CUSTOMER_STATEMENT_STATISTIC)) {
                        formEntityAddDTO.setName(I18nMessageUtil.getMessage(I18nStringConstant.DISTRIBUTOR_STATEMENT));
                        List<SummaryDataPoJo> summaryEntitys = new ArrayList<>();
                        summaryEntitys.add(new SummaryDataPoJo(CustomerStatementStatisticDistributorEnum.LINK_CUSTOMER.getAttr(), CustomerStatementStatisticEnum.LINK_CUSTOMER.getAttrName(), CustomerStatementStatisticEnum.LINK_CUSTOMER.getFieldType()));
                        summaryEntitys.add(new SummaryDataPoJo(CustomerStatementStatisticDistributorEnum.CHECK_DATE.getAttr(), CustomerStatementStatisticEnum.CHECK_DATE.getAttrName(), CustomerStatementStatisticEnum.CHECK_DATE.getFieldType()));
                        formEntityAddDTO.setSummaryEntity(summaryEntitys);
                    }
                }
                //默认启用，防止不启用的菜单覆盖
                formEntityAddDTO.setEnable(1);
                formEntityAddDTO.setDistributorMark(distributorMarkEnum.getCode());
                formList.add(formEntityAddDTO);
            }
        }catch (XbbException e) {
            throw e;
        }
        return formList;
    }
    public List<FormEntityAddDTO> getPrePaymentFormList(List<FormEntityAddDTO> formList, List<PaasMenuEntity> menuList) throws XbbException{
        try {
            for (PaasMenuEntity item : menuList) {
                FormEntityAddDTO formEntityAddDTO = new FormEntityAddDTO();
                BeanUtil.copyProperties(item, formEntityAddDTO);
                FundManagementMenuEnum menuEnum = FundManagementMenuEnum.getByAlias(item.getAlias());
                if (Objects.isNull(menuEnum)) {
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.BUSINESS_TYPE_NOT_EXIST);
                }
                if(MenuTypeEnum.GROUP.getType().equals(menuEnum.getType()) || MenuTypeEnum.MENU.getType().equals(menuEnum.getType())) {
                    continue;
                }
                formEntityAddDTO.setBusinessType(menuEnum.getCode());
                formEntityAddDTO.setSaasMark(SaasMarkEnum.SAAS.getCode());
                formEntityAddDTO.setCustomTitle(getAppDefaultTitle(menuEnum.getCode()));
                formEntityAddDTO.setLabels(getAppDefaultLabel(menuEnum.getCode()));
                formEntityAddDTO.setSummaryEntity(getAppDefaultSummary(menuEnum.getCode()));
                formEntityAddDTO.setBusinessRules("0");
                formEntityAddDTO.setMenuId(item.getId());
                formList.add(formEntityAddDTO);
                if (XbbRefTypeEnum.PAYMENT.getAlias().equals(item.getAlias())) {
                    formList.add(getFormEntityAddDTO(item, XbbRefTypeEnum.PAYMENT_SHEET));
                }
                if (XbbRefTypeEnum.PAY_PLAN.getAlias().equals(item.getAlias())) {
                    formList.add(getFormEntityAddDTO(item, XbbRefTypeEnum.PAY_SHEET));
                }
            }
        }catch (XbbException e) {
            throw e;
        }
        return formList;
    }

    /**
     * 得到客户对账单的form相关信息(如果生成过form，则不用继续处理)
     * @param corpidList
     * @param insertStatementMenuList
     * @return java.util.List<com.xbongbong.paas.pojo.dto.FormEntityAddDTO>
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    public List<PaasFormEntity> getCustomerStatementFormList(List<String> corpidList, List<PaasMenuEntity> insertStatementMenuList) {
        //corpid-对账单form（防止已经生成过form）
        Map<String, PaasFormEntityExt> corpStatementFormMap = getStatementFormMap(corpidList);
        List<PaasFormEntity> formList = new ArrayList<>();
        long now = DateUtil.getInt();
        for (PaasMenuEntity paasMenuEntity : insertStatementMenuList) {
            String corpid = paasMenuEntity.getCorpid();
            PaasFormEntityExt paasFormEntityExt = corpStatementFormMap.get(corpid);
            //不存在的才需要处理，存在的跳过
            if(paasFormEntityExt !=null){
                continue;
            }
            try {
                FormEntityAddDTO formEntityAddDTO = new FormEntityAddDTO();
                PaasFormEntity paasFormEntity = new PaasFormEntity();
                BeanUtil.copyProperties(formEntityAddDTO, paasFormEntity);
                BeanUtil.copyProperties(paasMenuEntity, paasFormEntity);
                FundManagementMenuEnum menuEnum = FundManagementMenuEnum.getByAlias(paasMenuEntity.getAlias());
                if (Objects.isNull(menuEnum)) {
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.BUSINESS_TYPE_NOT_EXIST);
                }
                if(MenuTypeEnum.GROUP.getType().equals(menuEnum.getType()) || MenuTypeEnum.MENU.getType().equals(menuEnum.getType())) {
                    continue;
                }
                paasFormEntity.setBusinessType(menuEnum.getCode());
                paasFormEntity.setBusinessRules("0");
                paasFormEntity.setHidden(0);
                paasFormEntity.setMenuId(paasMenuEntity.getId());
                paasFormEntity.setCoOpPerms(JSONObject.toJSONString(formEntityAddDTO.getCoOpPerms()));
                List<SummaryDataPoJo> summaryEntitys = new ArrayList<>();
                if (Objects.equals(menuEnum, FundManagementMenuEnum.CUSTOMER_STATEMENT_STATISTIC)) {
                    paasFormEntity.setName(I18nMessageUtil.getMessage(I18nStringConstant.CUSTOMER_STATEMENT_RECORD));
                    summaryEntitys.add(new SummaryDataPoJo(CustomerStatementStatisticEnum.LINK_CUSTOMER.getAttr(), CustomerStatementStatisticEnum.LINK_CUSTOMER.getAttrName(), CustomerStatementStatisticEnum.LINK_CUSTOMER.getFieldType()));
                    summaryEntitys.add(new SummaryDataPoJo(CustomerStatementStatisticEnum.CHECK_DATE.getAttr(), CustomerStatementStatisticEnum.CHECK_DATE.getAttrName(), CustomerStatementStatisticEnum.CHECK_DATE.getFieldType()));
                }
                paasFormEntity.setSummary(JSONObject.toJSONString(summaryEntitys));
                paasFormEntity.setLabels(JSONObject.toJSONString(formEntityAddDTO.getLabels()));
                paasFormEntity.setAssociationList(JSONObject.toJSONString(formEntityAddDTO.getAssociationList()));
                paasFormEntity.setSurcharge(JSONObject.toJSONString(formEntityAddDTO.getSurcharge()));
                paasFormEntity.setCustomTitle(JSONObject.toJSONString(formEntityAddDTO.getCustomTitle()));
                paasFormEntity.setAddTime(now);
                paasFormEntity.setUpdateTime(now);
                paasFormEntity.setIsProcessForm(BasicConstant.ZERO);
                paasFormEntity.setDel(0);
                paasFormEntity.setSort(0);
                formList.add(paasFormEntity);
            }catch (Exception e) {
                LOG.error("公司（corpid=" + corpid + "）的" + paasMenuEntity.getName() + "form初始化错误，InitFundManagementMenuHelp.getCustomerStatementFormList 出错", e);
            }
        }
        return formList;
    }

    /**
     * 获取corpid与对账单form对应关系map
     * @param corpidList
     * @return java.util.Map<java.lang.String,com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt>
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    private Map<String, PaasFormEntityExt> getStatementFormMap(List<String> corpidList) {
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("corpidIn", corpidList);
        param.put("businessTypeList", Arrays.asList(XbbRefTypeEnum.CUSTOMER_STATEMENT.getCode(), XbbRefTypeEnum.CUSTOMER_STATEMENT_STATISTIC.getCode()));
        param.put("del", 0);
        List<PaasFormEntityExt> list = paasFormModel.findEntitys(param);
        //corpid-对账单form
        Map<String, PaasFormEntityExt> corpStatementFormMap = list.stream().collect(Collectors.toMap(PaasFormEntityExt::getCorpid, Function.identity(),(k1, k2)->k1));
        return corpStatementFormMap;
    }

    /**
     * 根据xbbRefTypeEnum，获取FormEntityAddDTO
     * @param paasMenuEntity 菜单实体
     * @param xbbRefTypeEnum 业务类型
     * @return com.xbongbong.paas.pojo.dto.FormEntityAddDTO
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    private FormEntityAddDTO getFormEntityAddDTO(PaasMenuEntity paasMenuEntity, XbbRefTypeEnum xbbRefTypeEnum) {
        FormEntityAddDTO formEntityAddDTO = new FormEntityAddDTO();
        BeanUtil.copyProperties(paasMenuEntity, formEntityAddDTO);
        formEntityAddDTO.setBusinessType(xbbRefTypeEnum.getCode());
        formEntityAddDTO.setSaasMark(SaasMarkEnum.SAAS.getCode());
        formEntityAddDTO.setBusinessRules("0");
        formEntityAddDTO.setMenuId(paasMenuEntity.getId());
        formEntityAddDTO.setName(xbbRefTypeEnum.getName());
        return formEntityAddDTO;
    }

    /**
     * 获取付款单
     * @param paasMenuEntity
     * @return
     */
    private FormEntityAddDTO getPayPlanSheet(PaasMenuEntity paasMenuEntity) {
        FormEntityAddDTO formEntityAddDTO = new FormEntityAddDTO();
        BeanUtil.copyProperties(paasMenuEntity, formEntityAddDTO);
        formEntityAddDTO.setBusinessType(XbbRefTypeEnum.PAY_SHEET.getCode());
        formEntityAddDTO.setSaasMark(SaasMarkEnum.SAAS.getCode());
        formEntityAddDTO.setBusinessRules("0");
        formEntityAddDTO.setMenuId(paasMenuEntity.getId());
        formEntityAddDTO.setName(XbbRefTypeEnum.PAY_SHEET.getName());
        return formEntityAddDTO;
    }

    /**
     * CRM简化版-资金管理菜单初始化
     * @param menuInitDTO
     * @param mirrorObj 镜像配置
     * @author xingxing.xiao
     * @date 2019/11/28 20:32
     */
    public MenuInitVO initSimpleMenu(MenuInitDTO menuInitDTO, JSONObject mirrorObj) throws XbbException {
        MenuInitVO menuInitVO = new MenuInitVO();
        try {
            JSONObject menuObj = mirrorObj.getJSONObject(MirrorModuleEnum.MENU.getAlias());
            JSONArray fundMenu = menuObj.getJSONArray(SystemAppMenuEnum.FUND_MANAGEMENT.getAlias());
            JSONObject formObj = mirrorObj.getJSONObject(MirrorModuleEnum.FORM.getAlias());
            JSONArray fundForm = formObj.getJSONArray(SystemAppMenuEnum.FUND_MANAGEMENT.getAlias());
            JSONObject fieldObj = mirrorObj.getJSONObject(MirrorModuleEnum.FIELD.getAlias());

            Long appId = menuInitDTO.getAppMap().get(SystemAppMenuEnum.FUND_MANAGEMENT.getAlias()).getId();
            menuInitDTO.setAppId(appId);

            // 1. 插入菜单分组
            MenuGroupAddBatchVO crmGroupAddBatchVO = groupSimpleAddBatch(menuInitDTO, fundMenu);
            if(Objects.isNull(crmGroupAddBatchVO)){
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
            }
            // 插入除分组外对应的菜单(子菜单)
            MenuFormAddBatchVO menuFormAddBatchVO = formSimpleAddBatch(menuInitDTO, crmGroupAddBatchVO, fundMenu);
            if (Objects.isNull(menuFormAddBatchVO)) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
            }
            // 2. 插入菜单对应的表单
            List<PaasMenuEntity> menuGroupList = crmGroupAddBatchVO.getMenuList();
            List<PaasMenuEntity> menuList = menuFormAddBatchVO.getMenuList();
            menuList.addAll(menuGroupList);
            FormAddBatchDTO formAddBatchDTO = new FormAddBatchDTO();
            BeanUtil.copyProperties(menuInitDTO, formAddBatchDTO);
            List<FormEntityAddDTO> formList = new ArrayList<>();
            getSimpleFormList(formList, menuList, fundForm);
            formAddBatchDTO.setFormList(formList);
            FormAddBatchVO formAddBatchVO = paasFormService.addBatch(formAddBatchDTO);
            if (Objects.isNull(formAddBatchVO)) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
            }
            // 3. 插入表单对应的字段解释
            formExplainSimpleAddBatch(menuInitDTO, formAddBatchVO, fieldObj);
            menuInitVO.setFormList(formAddBatchVO.getFormList());
        } catch (XbbException e){
            throw e;
        } catch (Exception e){
            LOG.error("InitCrmMenuHelp.initSimpleMenu初始化菜单出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return menuInitVO;
    }

    /**
     * CRM简化版-批量添加分组
     * @param menuInitDTO
     * @param fundMenu 菜单镜像
     * @author xingxing.xiao
     * @date 2019/11/28 20:36
     */
    private MenuGroupAddBatchVO groupSimpleAddBatch(MenuInitDTO menuInitDTO, JSONArray fundMenu) throws XbbException {
        MenuGroupAddBatchVO menuGroupAddBatchVO;
        try {
            Long appId = menuInitDTO.getAppId();
            MenuGroupAddBatchDTO menuGroupAddBatchDTO = new MenuGroupAddBatchDTO();
            BeanUtil.copyProperties(menuInitDTO, menuGroupAddBatchDTO);
            menuGroupAddBatchDTO.setAppId(appId);
            List<MenuGroupAddDTO> groupMenuList = new ArrayList<>();
            for(FundManagementMenuEnum fundManagementMenuEnum: FundManagementMenuEnum.values()){
                if(fundManagementMenuEnum.getBelongAlias().equals(menuInitDTO.getAlias())
                        && StringUtil.isEmpty(fundManagementMenuEnum.getParent())) {
                    MenuGroupAddDTO menuGroupAddDTO = new MenuGroupAddDTO();
                    BeanUtil.copyProperties(menuInitDTO, menuGroupAddDTO);
                    menuGroupAddDTO.setParentId(0L);
                    menuGroupAddDTO.setAppId(appId);
                    menuGroupAddDTO.setSaasMark(SaasMarkEnum.SAAS.getCode());
                    menuGroupAddDTO.setAlias(fundManagementMenuEnum.getAlias());
                    menuGroupAddDTO.setName(fundManagementMenuEnum.getName());
                    AccessLinkEnum accessLinkEnum = fundManagementMenuEnum.getAccessLinkEnum();
                    menuGroupAddDTO.setIcon(accessLinkEnum.getIcon());
                    menuGroupAddDTO.setColor(accessLinkEnum.getColor());
                    menuGroupAddDTO.setUrl(accessLinkEnum.getUrl());
                    menuGroupAddDTO.setMobileUrl(accessLinkEnum.getMobileUrl());
                    menuGroupAddDTO.setType(fundManagementMenuEnum.getType());
                    if (fundMenu == null || !fundMenu.contains(fundManagementMenuEnum.getAlias())) {
                        // 镜像中没有配置的，默认关闭
                        menuGroupAddDTO.setEnable(0);
                    }
                    groupMenuList.add(menuGroupAddDTO);
                }
            }
            menuGroupAddBatchDTO.setMenuList(groupMenuList);
            menuGroupAddBatchVO = paasMenuService.addBatchGroupMenu(menuGroupAddBatchDTO);
        } catch (XbbException e) {
            throw e;
        } catch (Exception e) {
            LOG.error("InitCrmMenuHelp.groupSimpleAddBatch批量添加分组出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return menuGroupAddBatchVO;
    }

    /**
     * 批量添加子级菜单
     * @param menuInitDTO
     * @param menuGroupAddBatchVO
     * @return com.xbongbong.pro.menu.pojo.vo.MenuFormAddBatchVO
     * @throws XbbException
     */
    private MenuFormAddBatchVO formSimpleAddBatch(MenuInitDTO menuInitDTO, MenuGroupAddBatchVO menuGroupAddBatchVO, JSONArray fundMenu) throws XbbException {
        MenuFormAddBatchVO menuFormAddBatchVO = new MenuFormAddBatchVO();
        try {
            Long appId = menuInitDTO.getAppId();
            Map<String, PaasMenuEntity> menuMap = new HashMap<>(menuGroupAddBatchVO.getMenuList().size());
            menuGroupAddBatchVO.getMenuList().forEach((item)->{
                menuMap.put(item.getAlias(), item);
            });
            MenuFormAddBatchDTO crmFormAddBatchDTO = new MenuFormAddBatchDTO();
            BeanUtil.copyProperties(menuInitDTO, crmFormAddBatchDTO);
            crmFormAddBatchDTO.setAppId(appId);
            List<MenuFormAddDTO> crmFormMenuList = new ArrayList<>();
            for(FundManagementMenuEnum menuEnum: FundManagementMenuEnum.values()){
                if(menuEnum.getBelongAlias().equals(menuInitDTO.getAlias())
                        &&StringUtil.isNotEmpty(menuEnum.getParent())){
                    MenuFormAddDTO menuFormAddDTO = new MenuFormAddDTO();
                    BeanUtil.copyProperties(menuInitDTO, menuFormAddDTO);
                    menuFormAddDTO.setAppId(appId);
                    if(StringUtil.isEmpty(menuEnum.getParent())){
                        menuFormAddDTO.setParentId(0L);
                    }else{
                        menuFormAddDTO.setParentId(menuMap.get(menuEnum.getParent()).getId());
                    }
                    menuFormAddDTO.setSaasMark(SaasMarkEnum.SAAS.getCode());
                    menuFormAddDTO.setAlias(menuEnum.getAlias());
                    menuFormAddDTO.setName(menuEnum.getName());
                    AccessLinkEnum accessLinkEnum = menuEnum.getAccessLinkEnum();
                    menuFormAddDTO.setIcon(accessLinkEnum.getIcon());
                    menuFormAddDTO.setColor(accessLinkEnum.getColor());
                    menuFormAddDTO.setUrl(accessLinkEnum.getUrl());
                    menuFormAddDTO.setMobileUrl(accessLinkEnum.getMobileUrl());
                    menuFormAddDTO.setType(menuEnum.getType());
                    if (!fundMenu.contains(menuEnum.getAlias())) {
                        // 镜像中没有配置的，不开启
                        menuFormAddDTO.setEnable(BasicConstant.NO_USE);
                    }
                    crmFormMenuList.add(menuFormAddDTO);
                }
            }
            if (CollectionsUtil.isNotEmpty(crmFormMenuList)) {
                crmFormAddBatchDTO.setMenuList(crmFormMenuList);
                menuFormAddBatchVO = paasMenuService.addBatchFormMenu(crmFormAddBatchDTO);
            }
        } catch (XbbException e) {
            throw e;
        } catch (Exception e) {
            LOG.error("InitCrmMenuHelp.formAddBatch批量添加表单出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return menuFormAddBatchVO;
    }

    /**
     * CRM简化版-根据菜单获取表单列表
     * @param formList
     * @param menuList
     * @param fundForm 表单镜像
     * @author xingxing.xiao
     * @date 2019/11/28 20:48
     */
    private List<FormEntityAddDTO> getSimpleFormList(List<FormEntityAddDTO> formList, List<PaasMenuEntity> menuList, JSONArray fundForm) throws XbbException{
        try {
            for (PaasMenuEntity item : menuList) {
                FormEntityAddDTO formEntityAddDTO = new FormEntityAddDTO();
                BeanUtil.copyProperties(item, formEntityAddDTO);
                FundManagementMenuEnum menuEnum = FundManagementMenuEnum.getByAlias(item.getAlias());
                if (Objects.isNull(menuEnum)) {
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.BUSINESS_TYPE_NOT_EXIST);
                }
                if(MenuTypeEnum.GROUP.getType().equals(menuEnum.getType()) || MenuTypeEnum.MENU.getType().equals(menuEnum.getType())) {
                    continue;
                }
                formEntityAddDTO.setBusinessType(menuEnum.getCode());
                formEntityAddDTO.setSaasMark(SaasMarkEnum.SAAS.getCode());
                formEntityAddDTO.setCustomTitle(getAppDefaultTitle(menuEnum.getCode()));
                formEntityAddDTO.setLabels(initMenuHelp.getAppDefaultLabelSimple(menuEnum.getCode()));
                formEntityAddDTO.setSummaryEntity(initMenuHelp.getAppDefaultSummarySimple(menuEnum.getCode()));
                formEntityAddDTO.setBusinessRules("0");
                formEntityAddDTO.setMenuId(item.getId());
                if (fundForm == null || !fundForm.contains(menuEnum.getAlias())) {
                    // 镜像中没有配置的，默认关闭
                    formEntityAddDTO.setEnable(0);
                }
                formList.add(formEntityAddDTO);
                if (XbbRefTypeEnum.PAY_PLAN.getAlias().equals(item.getAlias())) {
                    formList.add(getPayPlanSheet(item));
                }
            }
        }catch (XbbException e) {
            throw e;
        }
        return formList;
    }

    /**
     * CRM简化版-添加表单字段解释
     * @param menuInitDTO
     * @param formAddBatchVO
     * @param fieldObj
     * @author xingxing.xiao
     * @date 2019/11/29 21:14
     */
    public FormExplainAddBatchVO formExplainSimpleAddBatch(MenuInitDTO menuInitDTO, FormAddBatchVO formAddBatchVO, JSONObject fieldObj) throws XbbException {
        FormExplainAddBatchVO formExplainAddBatchVO;
        try {
            List<PaasFormEntity> formResultList = formAddBatchVO.getFormList();
            FormExplainAddBatchDTO formExplainAddBatchDTO = new FormExplainAddBatchDTO();
            BeanUtil.copyProperties(menuInitDTO, formExplainAddBatchDTO);
            List<FormExplainAddDTO> formExplainList = new ArrayList<>();
            FundSetTransitionPojo fundSetTransitionPojo = new FundSetTransitionPojo(menuInitDTO.getCorpid());
            for (PaasFormEntity item : formResultList) {
                FormExplainAddDTO formExplainAddDTO = new FormExplainAddDTO();
                BeanUtil.copyProperties(item, formExplainAddDTO);
                formExplainAddDTO.setFormId(item.getId());
                String alias = XbbRefTypeEnum.getByCode(item.getBusinessType()).getAlias();
                // 获取表单对应的字段镜像
                JSONArray fieldArray = fieldObj.getJSONArray(alias);
                // 初始化方法获取字段列表
                formExplainAddDTO.setFormList(InitExplainsHelper.initSimpleFieldAttrEntity(alias, fieldArray, menuInitDTO.getProductForm(),fundSetTransitionPojo));
                //默认启用，防止不启用的菜单覆盖
                formExplainAddDTO.setEnable(1);
                formExplainList.add(formExplainAddDTO);
            }
            formExplainAddBatchDTO.setFormExplainList(formExplainList);
            formExplainAddBatchVO = paasFormExplainService.addBatch(formExplainAddBatchDTO);
        } catch (XbbException e) {
            throw e;
        } catch (Exception e) {
            LOG.error("InitCrmMenuHelp.formExplainSimpleAddBatch批量添加表单解释信息出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return formExplainAddBatchVO;
    }
    /**
     * 得到单模板的form相关信息(如果生成过form，则不用继续处理)
     * @param corpidList
     * @param insertMenuList form类型的菜单list
     * @return java.util.List<com.xbongbong.paas.pojo.dto.FormEntityAddDTO>
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    public List<PaasFormEntity> getSingleFormList(List<String> corpidList, List<PaasMenuEntity> insertMenuList, List<FundManagementMenuEnum> fundMenuEnumList) {
        List<Integer> businessTypeList = new ArrayList<>();
        for (FundManagementMenuEnum menuEnum : fundMenuEnumList) {
            //menu的code是用的XbbRefTypeEnum的code
            businessTypeList.add(menuEnum.getCode());
        }
        //corpid-对账单form（防止已经生成过form）
        Map<String, PaasFormEntityExt> corpSingleFormMap = getSingleFormMap(corpidList, businessTypeList);
        List<PaasFormEntity> formList = new ArrayList<>();
        long now = DateUtil.getInt();
        for (PaasMenuEntity paasMenuEntity : insertMenuList) {
            String corpid = paasMenuEntity.getCorpid();
            PaasFormEntityExt paasFormEntityExt = corpSingleFormMap.get(corpid);
            //不存在的才需要处理，存在的跳过；非form类型的菜单也跳过
            if(paasFormEntityExt !=null || !Objects.equals(paasMenuEntity.getType(), MenuTypeEnum.FORM.getType())){
                continue;
            }
            try {
                FormEntityAddDTO formEntityAddDTO = new FormEntityAddDTO();
                PaasFormEntity paasFormEntity = new PaasFormEntity();
                BeanUtil.copyProperties(formEntityAddDTO, paasFormEntity);
                BeanUtil.copyProperties(paasMenuEntity, paasFormEntity);
                FundManagementMenuEnum menuEnum = FundManagementMenuEnum.getByAlias(paasMenuEntity.getAlias());
                if (Objects.isNull(menuEnum)) {
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.BUSINESS_TYPE_NOT_EXIST);
                }
                if(MenuTypeEnum.GROUP.getType().equals(menuEnum.getType()) || MenuTypeEnum.MENU.getType().equals(menuEnum.getType())) {
                    continue;
                }
                paasFormEntity.setBusinessType(menuEnum.getCode());
                paasFormEntity.setBusinessRules("0");
                paasFormEntity.setMenuId(paasMenuEntity.getId());
                paasFormEntity.setCoOpPerms(JSONObject.toJSONString(formEntityAddDTO.getCoOpPerms()));
                List<SummaryDataPoJo> summaryEntitys = new ArrayList<>();
                switch (menuEnum) {
                    case OTHER_INCOME:
                        summaryEntitys.add(new SummaryDataPoJo(OtherIncomeEnum.BELONG_ID.getAttr(), OtherIncomeEnum.BELONG_ID.getAttrName(), OtherIncomeEnum.BELONG_ID.getFieldType()));
                        summaryEntitys.add(new SummaryDataPoJo(OtherIncomeEnum.INCOME_TIME.getAttr(), OtherIncomeEnum.INCOME_TIME.getAttrName(), OtherIncomeEnum.INCOME_TIME.getFieldType()));
                        summaryEntitys.add(new SummaryDataPoJo(OtherIncomeEnum.FUND_ACCOUNT.getAttr(), OtherIncomeEnum.FUND_ACCOUNT.getAttrName(), OtherIncomeEnum.FUND_ACCOUNT.getFieldType()));
                        break;
                    case OTHER_EXPENSE:
                        summaryEntitys.add(new SummaryDataPoJo(OtherExpenseEnum.BELONG_ID.getAttr(), OtherExpenseEnum.BELONG_ID.getAttrName(), OtherExpenseEnum.BELONG_ID.getFieldType()));
                        summaryEntitys.add(new SummaryDataPoJo(OtherExpenseEnum.EXPENSE_TIME.getAttr(), OtherExpenseEnum.EXPENSE_TIME.getAttrName(), OtherExpenseEnum.EXPENSE_TIME.getFieldType()));
                        summaryEntitys.add(new SummaryDataPoJo(OtherExpenseEnum.FUND_ACCOUNT.getAttr(), OtherExpenseEnum.FUND_ACCOUNT.getAttrName(), OtherExpenseEnum.FUND_ACCOUNT.getFieldType()));
                        break;
                    case FUND_TRANSFER:
                        summaryEntitys.add(new SummaryDataPoJo(FundTransferEnum.BELONG_ID.getAttr(), FundTransferEnum.BELONG_ID.getAttrName(), FundTransferEnum.BELONG_ID.getFieldType()));
                        summaryEntitys.add(new SummaryDataPoJo(FundTransferEnum.TRANSFER_TIME.getAttr(), FundTransferEnum.TRANSFER_TIME.getAttrName(), FundTransferEnum.TRANSFER_TIME.getFieldType()));
                        summaryEntitys.add(new SummaryDataPoJo(FundTransferEnum.OUT_FUND_ACCOUNT.getAttr(), FundTransferEnum.OUT_FUND_ACCOUNT.getAttrName(), FundTransferEnum.OUT_FUND_ACCOUNT.getFieldType()));
                        summaryEntitys.add(new SummaryDataPoJo(FundTransferEnum.IN_FUND_ACCOUNT.getAttr(), FundTransferEnum.IN_FUND_ACCOUNT.getAttrName(), FundTransferEnum.IN_FUND_ACCOUNT.getFieldType()));
                        break;
                    default:
                        break;
                }
                paasFormEntity.setSummary(JSONObject.toJSONString(summaryEntitys));
                paasFormEntity.setLabels(JSONObject.toJSONString(formEntityAddDTO.getLabels()));
                paasFormEntity.setAssociationList(JSONObject.toJSONString(formEntityAddDTO.getAssociationList()));
                paasFormEntity.setSurcharge(JSONObject.toJSONString(formEntityAddDTO.getSurcharge()));
                paasFormEntity.setCustomTitle(JSONObject.toJSONString(getAppDefaultTitle(menuEnum.getCode())));
                paasFormEntity.setAddTime(now);
                paasFormEntity.setUpdateTime(now);
                paasFormEntity.setIsProcessForm(BasicConstant.ZERO);
                paasFormEntity.setDel(0);
                paasFormEntity.setHidden(0);
                paasFormEntity.setSort(0);
                paasFormEntity.setDistributorMark(DistributorMarkEnum.OTHER.getCode());
                formList.add(paasFormEntity);
            }catch (Exception e) {
                LOG.error("公司（corpid=" + corpid + "）的" + paasMenuEntity.getName() + "form初始化错误，InitFundManagementMenuHelp.getSingleFormList 出错", e);
            }
        }
        return formList;
    }
    /**
     * 获取corpid与单模板的form对应关系map
     * @param corpidList
     * @param businessTypeList 单模板的业务类型list
     * @return java.util.Map<java.lang.String,com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt>
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    private Map<String, PaasFormEntityExt> getSingleFormMap(List<String> corpidList, List<Integer> businessTypeList) {
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("corpidIn", corpidList);
        param.put("businessTypeList", businessTypeList);
        param.put("del", 0);
        List<PaasFormEntityExt> list = paasFormModel.findEntitys(param);
        //corpid-form
        Map<String, PaasFormEntityExt> corpFormMap = list.stream().collect(Collectors.toMap(PaasFormEntityExt::getCorpid, Function.identity(),(k1, k2)->k1));
        return corpFormMap;
    }
}
